<?php
namespace Admin\Controller;
use Think\Controller;
header("Content-Type:text/html;charset=utf-8");
	class ProductController extends InitializeController{
		public function index(){
			$db = M('Product');
	        $count  = $db->count();// 查询满足要求的总记录数
	        $Page = new \Extend\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
	        $show = $Page->show();// 分页显示输出
	        $pages = $db->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id ASC')->select();
	        foreach ($pages as $k => $v) {
	        	$menu = M('Menu')->where("id=%d",$v['mid'])->field('name')->select();
	        	$pages["$k"]['menu'] = $menu['0']['name'];
	        }
	        $this->assign('model', $pages);
	        $this->assign('page',$show);
			$this->display();
		}

		public function add(){
			$User=D('Menu');
			$this->color = M('color')->select();
	        $arr = $User->sel_all();
	        //print_r($arr);die;
	        $this->assign('arr',$arr);
	        $this->display();
		}

		public function run_add(){
			if (I('submit') == '添加') {
				//图片上传
				$upload = new \Think\Upload();// 实例化上传类
	            $upload->maxSize = 3145728;// 设置附件上传大小
	            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型

	            // 上传文件01
	            $info = $upload->upload();
	            if(!$info) {// 上传错误提示错误信息
	                $this->error($upload->getError());
	            }else{// 上传成功
	                $data = array(
	                	'name' => I('name'),
						'content1' => I('content1'),
						'price1' => I('price1'),
						'content2' => I('content2'),
						'price2' => I('price2'),
	                    'photoname' => $info['photo']['savename'],
	                    'photourl' => $info['photo']['savepath'],
	                    'mid' => I('mid'),
	                    // 'detailsP' => $info['detailsP']['savename'],
	                    'detailsC' => strip_tags(I('detailsC')),
	                );



	            	//图片上传-end
	                if ($judge = M('Product')->data($data)->add()) {
	                	$where['photoname'] = $info['photo']['savename'];
	                	$id = M('Product')->where($where)->field('id')->select();
	                	for ($i=1; $i <4 ; $i++) {
		                	$detailsData = array(
		                		'pid' => $id['0']['id'],
	                    		'photoname' => $info["detailsP$i"]['savename'],
		                		'photourl' => $info["detailsP$i"]['savepath'],
		                		);
		                	$judgeDet = M('Detailedpic')->data($detailsData)->add();
		                }
		                if ($judgeDet) {
		                	$pid = M('Product')->order('id desc')->field('id')->select();
		                	$colorArr = I('color');
			                foreach ($colorArr as $v) {
			                	$color = array(
				                	'pid' => $pid['0']['id'],
				                	'cid' => $v['color'],
			                	);
			                	M('pro_col')->data($color)->add();
			                }

							$this->success('添加成功!',U('Product/index'));
		                }else{
		                	$this->error('详细信息图片错误');
		                }
					}else{
						$this->error('商品图片错误!');
					}
	            }
			}

		}

		public function delete(){
			$db = M('Product');
			if ($db->where("id=%d",I('id'))->delete()) {
				$this->success('删除成功!');
			}else{
				$this->error('访问错误!');
			}
		}

		public function checkout(){
			$db = M('checkout');
			$uDb = M('user');
			$proDb = M('Product');
			$checkout = $db->group('user_id')->select();
			foreach ($checkout as $k => $v){
				$group_user[$k] = $db->where("user_id=$v[user_id]")->select();
			}
			foreach ($group_user as $gk => $gv){
				foreach ($gv as $vk => $vv){
					$user_name = $uDb->where("user_id=$vv[user_id]")->select();
                    $prod = $proDb->where("id=$vv[pid]")->select();
					$result[$vk]['username'] = $user_name[0]['username'];
					$result[$vk]['time'] = $vv['time'];
					$result[$vk]['allPrice'] = $vv['price'];
					$result[$vk]['prod'] = $prod[0]['name'].$prod[0]['content1'];
				}
			}
			$this->assign('checkout',$result);
			$this->display();
		}

		public function excel(){
			$data = array(array('编号', '商城用户名', '所购物品', '时间', '总价'));
            for ($i=1; $i<=count(I('id')); $i++) {
							//$data["$i"]['0'] = I('id')["$i"];这个写法5.几的php不支持估计
							$data["$i"]['0'] = $_POST['id']["$i"];
							$data["$i"]['1'] = $_POST['username']["$i"];
            	$data["$i"]['2'] = $_POST['prod']["$i"];
            	$data["$i"]['3'] = $_POST['time']["$i"];
            	$data["$i"]['4'] = $_POST['allPrice']["$i"];
            }

	    	create_xls($data);
		}

		public function update(){
            $User=D('Menu');
            $id = $_GET['id'];
            $this->color = M('color')->select();
			$this->proInfo = M('product')->where("id=$id")->select();
            $arr = $User->sel_all()	;
            $this->assign('arr',$arr);
            $this->assign('proInfo',$this->proInfo[0]);
			$this->display();
		}

		public function pri($data)
		{
			echo '<pre>';
			print_r($data);
            echo '<pre>';
		}

		public function run_update()
		{
			$data = $_POST;
			$res = M('product')->where("id=$data[id]")->save($data);
			if ($res){
                $this->success('修改成功！',U('Product/index'));
            }else{
                $this->error('修改失败！',U('Product/index'));
            }
		}
	}
 ?>
